<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel backend\modules\wechat\models\WechatMenusSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
use common\models\wechat\WechatFansGroups;
$this->title = '微信菜单';
$this->params['breadcrumbs'][] = $this->title;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
            <?= Html::a('返回', 'javascript:history.back(-1)'); ?>  <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">

                <div class="wechat-menus-index">

                    <p>
                        <?= Html::a('添加菜单', ['create'], ['class' => 'btn btn-success']) ?>
                    </p>
                    <?= GridView::widget([
                        'dataProvider' => $dataProvider,
                        'filterModel' => $searchModel,
                        'columns' => [
                            'id',
                            'title',
                            [
                                'attribute' => 'type',
                                'value' => function ($model) {
                                    return \common\models\wechat\WechatMenus::$types[$model->type];
                                }
                            ],
                            [
                                'attribute' => 'matchruletext',
                                'format' => 'raw',
                                'value' => function ($model) {
                                    $text = '';
                                    $list = (new WechatFansGroups())->getFansGroups();
                                    foreach ($list['tags'] as $key => $row){
                                        if($row['id'] == $model->group_id){
                                            $text .= $row['name'].',';
                                        }
                                    }
                                    $text = Html::button($text,['class'=>'btn btn-primary btn-xs']);
                                    $text .= $model->area?Html::button($model->area,['class'=>'btn btn-info btn-xs']):'';
                                    $text .= $model->sex?Html::button(\common\models\wechat\WechatMenus::$sexs[$model->sex],['class'=>'btn btn-success btn-xs']):'';
                                    $text .= $model->client_platform_type?Html::button('手机'.\common\models\wechat\WechatMenus::$platform[$model->client_platform_type],['class'=>'btn btn-wanging btn-xs']):'';
                                    return $model->type == 3?$text:'';
                                }
                            ],
                            [
                                'attribute' => 'createtime',
                                'value' => function($model){
                                    return date('Y-m-d H:i:s',$model->createtime);
                                }
                            ],
                            [
                                'class' => 'yii\grid\ActionColumn',
                                'template' => '{edit}{delete}',
                                'buttons' => [
                                    'edit' => function ($url, $model, $key) {
                                            return Html::a('<span class="glyphicon glyphicon-edit"></span>', \yii\helpers\Url::to(['/wechat/menus/update', 'type' => $model->type, 'id' => $model->id]), ['title' => '修改']);
                                    },
                                    'delete' => function ($url, $model, $key) {
                                        if($model->type != 1 && $model->type != 2){
                                            return Html::a('<span class="glyphicon glyphicon-trash delete"></span>', '#', ['title' => '删除','id'=>'delmenu','data-type'=>$model->type,'data-id'=>$model->id]);
                                        }
                                    },
                                ],
                            ]
                        ],
                    ]); ?>
                </div>
            </div>
        </div>
    </section>
</section>



<script>
    <?php $this->beginBlock('js') ?>


    $('#delmenu').click(function(){
        if(!confirm('确认删除？')){
            return false;
        }

        $.post('/wechat/menus/delete',{id:$(this).data('id')},function(data){
            alert(data.msg);

            if(data.status == 0){
                window.location.href= '/wechat/menus/index';
            }
        },'json')


    })


//    function deletemenus(type,id){
//        if(!confirm('确认删除？')){
//            return false;
//        }
//        window.location.href= '/wechat/menus/delete?type='+type+'&id='+id;
//    }



    <?php $this->endBlock('js') ?>
    <?php $this->registerJs($this->blocks['js'],\yii\web\View::POS_END) ?>


</script>